#!/usr/bin/env python
# -*- coding:utf-8 -*-
## src/tray.py
##
## Copyright (C) 2009 Fernando Ribeiro <fernando dot ribeiro at gmail.com>
##
## This file is part of WatchPage.
##
## WatchPage is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## WatchPage is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
##

import os
import gtk

class TrayIcon(gtk.StatusIcon):
    def __init__(self):
        gtk.StatusIcon.__init__(self)
        menu = '''
            <ui>
             <menubar name="Menubar">
              <menu action="Menu">
               <menuitem action="Preferences"/>
               <separator/>
               <menuitem action="About"/>
              </menu>
             </menubar>
            </ui>
        '''
        actions = [
            ('Menu',  None, 'Menu'),
            ('Preferences', gtk.STOCK_PREFERENCES, '_Preferences...', None, 'Change Watch Page preferences', self.on_preferences),
            ('About', gtk.STOCK_ABOUT, '_About...', None, 'About Watch Page', self.on_about)]
        ag = gtk.ActionGroup('Actions')
        ag.add_actions(actions)
        self.manager = gtk.UIManager()
        self.manager.insert_action_group(ag, 0)
        self.manager.add_ui_from_string(menu)
        self.menu = self.manager.get_widget('/Menubar/Menu/About').props.parent
        self.set_from_stock(gtk.STOCK_FIND)
        self.set_tooltip('Watch Web Pages')
        self.set_visible(True)
        self.connect('activate', self.on_activate)
        self.connect('popup-menu', self.on_popup_menu)

    def on_activate(self, data):
        os.spawnlpe(os.P_NOWAIT, 'tracker-search-tool', os.environ)

    def on_popup_menu(self, status, button, time):
        self.menu.popup(None, None, None, button, time)

    def on_preferences(self, data):
        print 'preferences'

    def on_about(self, data):
        dialog = gtk.AboutDialog()
        dialog.set_name('Watch Page')
        dialog.set_version('0.1.0')
        dialog.set_comments('A watch web pages tool')
        dialog.set_website('www.nerdgroup.org/projects/watchpage')
        dialog.run()
        dialog.destroy()

